<?php


namespace app\crontab\controller\once;


use custom\Mongo;
use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * 小学初中映射
 * Class SchoolPrimaryMiddle
 * @package app\crontab\controller
 */
class SchoolPrimaryMiddle extends Command {

    public static $xx = 'wx_xqf_xx_details_data';
    public static $cz = 'wx_xqf_cz_details_data';

    protected function configure() {
        $this->setName('SchoolPrimaryMiddle');
    }

    protected function execute(Input $input, Output $output) {
        echo "start\n";
        $mongo = Mongo::getInstance();
        $int = 1;
        $options = [
            'projection' => ['_id' => 1, 'middle_schools' => 1],
            'sort'       => ['id' => 1],
        ];
        $cursor = $mongo->select(self::$xx, [], $options);
        foreach ($cursor as $v1) {
            $v1 = (array)$v1;
            if ($v1['middle_schools']) {
                foreach ($v1['middle_schools'] as $v2) {
                    $cz_name = $v2->name;
                    $cz_school = $mongo->select(self::$cz, ['name' => ['$eq' => $cz_name]], []);
                    if (!$cz_school) {
                        $cz_id = 0;
                    } else {
                        $cz_id = $cz_school[0]->_id;
                    }
                    $mongo->add(
                        [
                            '_id'     => (int)$int,
                            'xx_id'   => (string)$v1['_id'],
                            'cz_id'   => (string)$cz_id,
                            'cz_name' => (string)$cz_name
                        ],
                        'SchoolPrimaryMiddle'
                    );
                    $int++;
                }
            }
        }
        echo "end\n";
    }

}